package com.tucc.hello.leetcode.no1_400.no1_200.no160;

import com.tucc.hello.leetcode.common.ListNode;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        ListNode node1 = ListNode.of(5, 6, 1);
        ListNode node2 = ListNode.of(4, 1);
        ListNode node3 = ListNode.of(8, 4, 5);
        ListNode node1Tail = node1;
        while(node1Tail.next != null){
            node1Tail = node1Tail.next;
        }
        node1Tail.next = node3;
        ListNode node2Tail = node2;
        while(node2Tail.next != null){
            node2Tail = node2Tail.next;
        }
        node2Tail.next = node3;
        ListNode.print(node1);
        ListNode.print(node2);
        ListNode node4 = solution.getIntersectionNode(node1, node2);
        System.out.println(node3 == node4);
        System.out.println(node4 != null ? node4.val : null);

    }
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode tempA = headA, tempB = headB;
        while(tempA != tempB){
            tempA = tempA == null ? headB : tempA.next;
            tempB = tempB == null ? headA : tempB.next;
        }
        return tempA;
    }

}
